<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form label-width="80px" :inline="true">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          clearable
          filterable
          v-model="(category1Id as number)"
          @change="getCategory2List"
        >
          <el-option
            :label="category1.name"
            :value="category1.id"
            v-for="category1 in category1List"
            :key="category1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          clearable
          filterable
          v-model="(category2Id as number)"
          @change="getCategory3List"
        >
          <el-option
            :label="category2.name"
            :value="category2.id"
            v-for="category2 in category2List"
            :key="category2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          clearable
          filterable
          v-model="(category3Id as number)"
        >
          <el-option
            :label="category3.name"
            :value="category3.id"
            v-for="category3 in category3List"
            :key="category3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, onMounted } from "vue";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
// 使用pinia数据
import useCategortStore from "@/stores/category";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";
const categoryStore = useCategortStore();
const { getCategory1List, getCategory2List, getCategory3List } = categoryStore;
const {
  category1List,
  category1Id,
  category2List,
  category2Id,
  category3List,
  category3Id,
} = storeToRefs(categoryStore);
// 初始化请求一级分类列表
onMounted(() => {
  try {
    getCategory1List();
  } catch (e: any) {
    ElMessage.error("平台属性列表请求失败:" + e.message);
  }
});
</script>

<style scoped></style>
